<template>
  <el-form :model="coupon" label-width="auto" style="max-width: 800px">
    <el-row gutter={100}>
      <!-- 优惠券名称 -->
      <el-col :span="6">
        <el-form-item label="优惠券名称">
          <el-input v-model="coupon.couponName" placeholder="请输入优惠券名称" />
        </el-form-item>
      </el-col>

      <!-- 活动状态 -->
      <el-col :span="7">
        <el-form-item label="活动状态">
          <el-select v-model="coupon.couponStatus" placeholder="请选择活动状态">
            <el-option label="全部" value="" />
            <el-option label="未开始" value="1" />
            <el-option label="进行中" value="2" />
            <el-option label="已失效" value="3" />
            <el-option label="已结束" value="4" />
          </el-select>
        </el-form-item>
      </el-col>

      <!-- 优惠券类型 -->
      <el-col :span="8">
        <el-form-item label="优惠券类型">
          <el-select v-model="coupon.couponType" placeholder="请选择优惠券类型">
            <el-option label="全部" value="" />
            <el-option label="满减券" value="1" />
            <el-option label="折扣券" value="2" />
            <el-option label="随机金额券" value="3" />
          </el-select>
        </el-form-item>
      </el-col>

      <!-- 时间区间 -->
      <el-col :span="6">
        <el-form-item label="活动时间">
          <el-row gutter={44}>
            <el-col :span="21">
              <el-date-picker
                  v-model="coupon.enableStartTime"
                  type="datetime"
                  placeholder="开始时间"
                  format="YYYY/MM/DD HH:mm:ss"
                  style="width: 100%;"
              />
            </el-col>
            <el-col :span="2" style="text-align: center; line-height: 32px;">-</el-col>
            <el-col :span="21">
              <el-date-picker
                  v-model="coupon.enableEndTime"
                  type="datetime"
                  placeholder="结束时间"
                  format="YYYY/MM/DD HH:mm:ss"
                  style="width: 100%;"
              />
            </el-col>
          </el-row>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="使用时间">
          <el-row gutter={44}>
            <el-col :span="21">
              <el-date-picker
                  v-model="coupon.effectiveStart"
                  type="datetime"
                  placeholder="开始时间"
                  format="YYYY/MM/DD HH:mm:ss"
                  style="width: 100%;"
              />
            </el-col>
            <el-col :span="2" style="text-align: center; line-height: 32px;">-</el-col>
            <el-col :span="21">
              <el-date-picker
                  v-model="coupon.effectiveEnd"
                  type="datetime"
                  placeholder="结束时间"
                  format="YYYY/MM/DD HH:mm:ss"
                  style="width: 100%;"
              />
            </el-col>
          </el-row>
        </el-form-item>
      </el-col>
      <!-- 查询按钮 -->
      <el-col :span="24">
        <el-form-item>
          <el-button type="primary" @click="searchCouponList">查询</el-button>
          <el-button type="primary" @click="unpack">新建优惠券</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column label="序号" width="120">
      <template #default="scope" align="center">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="优惠券名称" width="110"align="center">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.couponName }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="优惠券类型" width="110"align="center">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px" v-if="scope.row.couponType == 1">满减券</span>
          <span style="margin-left: 10px" v-if="scope.row.couponType == 2">折扣券</span>
          <span style="margin-left: 10px" v-if="scope.row.couponType == 3">随机金额券</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="优惠内容" width="200"align="center">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px" v-if="scope.row.couponType == 1">{{ '满' + scope.row.useThreshold + '元' + '减' + scope.row.minusPrice + '元' }}</span>
          <span style="margin-left: 10px" v-if="scope.row.couponType == 2">{{ '满' + scope.row.useThreshold + '元' + '打' + scope.row.discountNum + '折' }}</span>
          <span style="margin-left: 10px" v-if="scope.row.couponType == 3">{{ '满' + scope.row.useThreshold + '元' + '随机减' + scope.row.maxDiscount + '元' }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="已领取/已使用/发布数" width="150"align="center">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.grantNum }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="活动时间" width="240"align="center">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.enableStartTime }}</span>
          <br>~<br>
          <span style="margin-left: 10px">{{ scope.row.enableEndTime }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="使用时间" width="240" align="center">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.effectiveStart }}</span> ~
          <span style="margin-left: 10px">{{ scope.row.effectiveEnd }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="活动状态" width="100" align="center">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px" v-if="scope.row.couponStatus == 1">未开始</span>
          <span style="margin-left: 10px" v-if="scope.row.couponStatus == 2">进行中</span>
          <span style="margin-left: 10px" v-if="scope.row.couponStatus == 3">已失效</span>
          <span style="margin-left: 10px" v-if="scope.row.couponStatus == 4">已结束</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button size="small" type="danger" v-if="scope.row.couponStatus == 2" @click="couponView(scope.row)">
          查看
        </el-button>
        <el-button size="small" type="danger" v-if="scope.row.couponStatus == 2" @click="couponCopy(scope.row)">
          复制
        </el-button>
        <el-button size="small" type="danger" v-if="scope.row.couponStatus == 2" @click="couponLoseEfficacy(scope.row)">
          失效
        </el-button>
        <el-button size="small" type="danger" v-if="scope.row.couponStatus == 1" @click="couponRedact(scope.row)">
          编辑
        </el-button>
        <el-button size="small" type="danger" v-if="scope.row.couponStatus == 3" @click="couponDelete(scope.row)">
          删除
        </el-button>
      </template>
    </el-table-column>

  </el-table>

  <el-pagination
      v-model:current-page="coupon.pageNum"
      v-model:page-size="coupon.pageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="coupon.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
  />
  <!--新建优惠券/复制-->

  <el-dialog :v-model="dialogTableVisible" :title="title" width="500">
    <el-form :model="couponVo">
      <el-form-item label="" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="Zones" :label-width="formLabelWidth">
        <el-select v-model="form.region" placeholder="Please select a zone">
          <el-option label="Zone No.1" value="shanghai" />
          <el-option label="Zone No.2" value="beijing" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">
          Confirm
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getCouponList ,updCouponStatusById ,couponDelById} from "@/api/merchant/coupon";

export default {
  data() {
    return {
      tableData: [], // 用来存储数据
      coupon: {
        pageNum: 1,
        pageSize: 3,
        total: 0,
      },
      dialogTableVisible:false,
      title:'',
      couponVo:{}
    };
  },
  methods: {
    // 获取优惠券列表
    couponList() {
      getCouponList(this.coupon).then((res) => {
        this.tableData = res.data.list;
        this.coupon.total = res.data.total;
      });
    },

    // 处理分页大小变化
    handleSizeChange(value) {
      this.coupon.pageSize = value;
      this.couponList(); // 确保调用正确的函数名
    },

    // 处理当前页变化
    handleCurrentChange(value) {
      this.coupon.pageNum = value;
      this.couponList(); // 确保调用正确的函数名
    },

    // 搜索优惠券列表
    searchCouponList() {
      this.coupon.pageNum = 1;
      this.couponList();
    },
    //修改状态为失效
    couponLoseEfficacy(row) {
      updCouponStatusById(row.id).then(res =>{
        if (res.data.code==200){
          this.couponList()
        }
      })
    },
    //删除优惠券
    couponDelete(row) {
      couponDelById(row.id).then(res =>{
        if(res.data.code==200){
          this.couponList()
        }
      })
    },
    //打开查看优惠券
    couponView(row) {
      this.dialogTableVisible = true
      this.title = '优惠券详情'
    },
    //打开新建优惠券弹出框
    unpack() {
      this.dialogTableVisible = true
      this.title = '发布优惠券'
    },
    //打开复制优惠券弹出框
    couponCopy(row) {
      this.dialogTableVisible = true
      this.title = '发布优惠券',
      this.couponVo = row.value
    },
    //打开编辑优惠券弹出框
    couponRedact(row) {
      this.dialogTableVisible = true
      this.title = '编辑优惠券',
      this.couponVo = row.value
    },
  },
  mounted() {
    // 组件加载后获取优惠券列表
    this.couponList();
  },
};
</script>

<style scoped lang="scss">

</style>
